﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common;

namespace WoodManager.UI
{
    public partial class CopyModule : Form
    {
        public string sNhomModule, sNhomModule2, sModuleID;
        public DataTable tbModule;

        int iLoad = 0;

        ConnectManager cm = new ConnectManager();


        public CopyModule()
        {
            InitializeComponent();
            loadDataGrid();
            iLoad = 1;

            DataTable tbNhomModule2 = cm.getTable("Select * from NhomModule2 where NhomModuleID = N'" + cbbNhomModule.SelectedValue.ToString() + "'");
            cbbNhomModule2.DataSource = tbNhomModule2;
            cbbNhomModule2.DisplayMember = "TenNhomModule2";
            cbbNhomModule2.ValueMember = "NhomModule2ID";
            iLoad = 2;

        }

        private void loadDataGrid()
        {
            DataTable tbNhomModule = cm.getTable("Select * from NhomModule");
            cbbNhomModule.DataSource = tbNhomModule;
            cbbNhomModule.DisplayMember = "TenNhomModule";
            cbbNhomModule.ValueMember = "NhomModuleID";

            DataTable tbChiCanh = cm.getTable("Select * from ChiCanh");
            cbbChiCanh.DataSource = tbChiCanh;
            cbbChiCanh.DisplayMember = "TenChiCanh";
            cbbChiCanh.ValueMember = "IDChiCanh";

            DataTable tbSonPhu = cm.getTable("select IDSon, NhomSon, ChiTiet from SonPhu order by NhomSon ASC");
            cbbSonPhu.DataSource = tbSonPhu;
            cbbSonPhu.DisplayMember = "ChiTiet";
            cbbSonPhu.ValueMember = "IDSon";
        }

        private void cbbNhomModule_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iLoad > 0)
            {
                DataTable tbNhomModule2 = cm.getTable("Select * from NhomModule2 where NhomModuleID = N'"+cbbNhomModule.SelectedValue.ToString()+"'");
                cbbNhomModule2.DataSource = tbNhomModule2;
                cbbNhomModule2.DisplayMember = "TenNhomModule2";
                cbbNhomModule2.ValueMember = "NhomModule2ID";
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ((txtMaModule.Text == "") || (txtTenModule.Text == ""))
            {
                MessageBox.Show("Mã và tên Module không được để trống");
                return;
            }

            tbModule = cm.getTable("Select * from ModuleDetails where ModuleID = N'" + cbbModuleNguon.SelectedValue.ToString() + "'");

            try
            {
                if (cm.conn.State == ConnectionState.Closed)
                    cm.conn.Open();

                string sInsertNhomModule2 = "Insert into Module (ModuleID, TenModule, NhomModule2ID) values (N'" + txtMaModule.Text + "',N'" + txtTenModule.Text + "',N'" + cbbNhomModule2.SelectedValue.ToString() + "')";
                SqlCommand sqlInsertNhomModule2 = new SqlCommand(sInsertNhomModule2, cm.conn);
                sqlInsertNhomModule2.ExecuteNonQuery();

            }
            catch (SqlException sqlex)
            {
                MessageBox.Show(sqlex.Message);
                return;
            }

            try
            {
                for (int i = 0; i < tbModule.Rows.Count; i++)
                {
                    string sInsert0 = "InsertModuleDetails N'" + txtMaModule.Text + "', N'" + tbModule.Rows[i]["ChiTietID"].ToString() + "',";
                    string sInsert1 = "N'" + tbModule.Rows[i]["MaLoiVan"].ToString() + "', N'" + tbModule.Rows[i]["NhomVan"].ToString() + "' , N'" + tbModule.Rows[i]["SoLuong"].ToString() + "',";
                    string sInsert2 = "N'" + this.cbbSonPhu.SelectedValue.ToString() + "',N'" + cbbSonPhu.SelectedValue.ToString() + "',";
                    string sInsert3 = "N'" + this.cbbChiCanh.SelectedValue.ToString() + "',N'" + tbModule.Rows[i]["SoChiCanh"].ToString() + "'";
                    string sInsert = sInsert0 + sInsert1 + sInsert2 + sInsert3;

                    SqlCommand sqlInsert = new SqlCommand(sInsert, cm.conn);

                    sqlInsert.ExecuteNonQuery();

                }
                if (cm.conn.State == ConnectionState.Open)
                    cm.conn.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void cbbNhomModule2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iLoad > 1)
            {
                if (cm.conn.State == ConnectionState.Closed)
                    cm.conn.Open();
                DataTable tbModuleNguon = cm.getTable("Select * from Module where NhomModule2ID = N'"+cbbNhomModule2.SelectedValue.ToString()+"'");
                cbbModuleNguon.DataSource = tbModuleNguon;
                cbbModuleNguon.DisplayMember = "TenModule";
                cbbModuleNguon.ValueMember = "ModuleID";
            }
        }



        public delegate void loadTreeView();
        public loadTreeView loadTree;

        private void button2_Click(object sender, EventArgs e)
        {
            if (loadTree != null)
                loadTree();
            this.Dispose();
        }
    }
}
